package com.gdk.ksoft.ui.works.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.data.entry.tables.GoodsTable;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * Created by hel on 2014-11-27.
 * 查找商品的页面的商品列表适配器
 */
public class PfindGoodsAdapter extends BaseAdapter {

    private Context context;
    public List<GoodsTable> lst;

    public PfindGoodsAdapter(Context context, List<GoodsTable> lst) {
        this.context = context;
        this.lst = lst;
    }

    @Override
    public int getCount() {
        return lst == null?0: lst.size();
    }

    @Override
    public Object getItem(int position) {
        return lst.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GoodsTable item = lst.get(position);
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_pfindgoods_lstgoods,null);
            holder.imgIcon = (ImageView)convertView.findViewById(R.id.item_pfindgoods_lstgoods_imgIcon);
            holder.txtTitle = (TextView)convertView.findViewById(R.id.item_pfindgoods_lstgoods_txtTitle);
            holder.txtSmallTitle = (TextView)convertView.findViewById(R.id.item_pfindgoods_lstgoods_txtSmallTitle);
            holder.txtContent = (TextView)convertView.findViewById(R.id.item_pfindgoods_lstgoods_txtContext);
            holder.txtKc  = (TextView)convertView.findViewById(R.id.item_pfindgoods_lstgoods_txtKc);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        try {
            holder.txtTitle.setText(item.mc);
            holder.txtSmallTitle.setText(item.gg + item.dw);
            holder.txtContent.setText(item.tm);
            String kcStr = "";
            int js = (int)(item.kc / item.mjsl);
            int ss = (int)(item.kc % item.mjsl);
            if(js>0) kcStr += js + "件";
            if(ss>0) kcStr += ss + "散";
            holder.txtKc.setText(kcStr);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            new BitmapUtils(context).configDefaultLoadFailedImage(R.drawable.ic_default_goods)
                    .configDefaultLoadingImage(R.drawable.ic_default_goods)
                    .display(holder.imgIcon, SystemData.BASE_URL + item.slt);
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        public ImageView imgIcon;
        public TextView txtTitle;
        public TextView txtSmallTitle;
        public TextView txtContent;
        public TextView txtKc;
    }

}
